//
//  MineQueryDetailInfoViewController.m
//  HZHome
//
//  Created by SunShine on 13-10-13.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "MineQueryDetailInfoViewController.h"

@interface MineQueryDetailInfoViewController ()
@property (strong, nonatomic) NSString *phoneToCall;
@end

@implementation MineQueryDetailInfoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"详细资料";
    _dataItem = [_config objectForKey:@"dataItem"];
    if (_dataItem.content && _dataItem.content != (id)[NSNull null]&& _dataItem.content.length >0) {
        self.detailTextView.text = _dataItem.content;
        //计算高度
        CGFloat textHeight = [self caculateHeightForString:_dataItem.content AndFontSize:14.0 AndWitdh:_detailTextView.contentSize.width];
        NSLog(@"计算出textView高度为%f",textHeight);
        //扩展textview的高度
        _detailTextView.frame = CGRectMake(_detailTextView.frame.origin.x, _detailTextView.frame.origin.y, _detailTextView.frame.size.width, textHeight);
        //调整按钮的位置
        CGRect panelFrame = self.buttonsPanel.frame;
        panelFrame.origin.y = CGRectGetMaxY(_detailTextView.frame);
        self.buttonsPanel.frame = panelFrame;
        //ScrollView的整体高度
        CGFloat totalHeight = CGRectGetMaxY(_buttonsPanel.frame);
        NSLog(@"计算出的总高度为%f",totalHeight);
        _scrollView.contentSize = CGSizeMake(320,  totalHeight);
    } else {
        self.detailTextView.text = @"无详细资料";
        _scrollView.scrollEnabled = YES;
    }
    if (_dataItem.phone && _dataItem.phone!=(id)[NSNull null] && _dataItem.phone.length > 0) {
        [self.phoneButton setTitle:_dataItem.phone forState:UIControlStateNormal];
        [self.phoneButton setTitleColor:self.phoneButton.titleLabel.textColor forState:UIControlStateHighlighted];
        [self.phoneButton addTarget:self action:@selector(phoneButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    } else {
        [self.phoneButton setTitle:@"" forState:UIControlStateNormal];
        self.phoneButton.enabled = NO;
    }
    
    self.addressTextView.text = _dataItem.address;
    if ([_dataItem respondsToSelector:@selector(busLines)] && _dataItem.busLines && _dataItem.busLines!=(id)[NSNull null] && _dataItem.busLines.length > 0) {
        self.busLabel.text=_dataItem.busLines;
    }
    else
    {
        self.imgBus.hidden=YES;
        self.busLabel.hidden=YES;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)phoneButtonAction:(id)sender
{
    NSString* phone = _dataItem.phone;
    if ([phone rangeOfString:@","].location != NSNotFound) {
        NSLog(@"准备截断电话号码%@",phone);
        phone = [phone substringToIndex:[phone rangeOfString:@","].location];
    }
    NSLog(@"点击了电话按钮%@",phone);
//    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",phone]]];
    self.phoneToCall = phone;
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString  stringWithFormat:@"您是否需要拨打电话%@",phone] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alert.tag = PHONE_ALERT_TAG;
    [alert show];
}

#pragma mark - Alert View Delegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == PHONE_ALERT_TAG) {
        if (buttonIndex == 1) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",_phoneToCall]]];
        }
    }
}
@end
